﻿Public Class Dashboard1
    Inherits System.Web.UI.Page

    Public dbManager As DBManager = New DBManager

    'To set masterpage 
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        'Check if user is null
        If (Session("user") IsNot Nothing) Then
            'Retrieve userAcc details from session
            Dim userLoggedIn As userAcc = Session("user")
            'Check if user is admin, assign masterpage respectively
            If userLoggedIn.isAdmin Then
                Me.MasterPageFile = "~/MasterPage/Admin.Master"
            Else
                Me.MasterPageFile = "~/MasterPage/User.Master"
            End If
        Else
            'For anonymous users
            Me.MasterPageFile = "~/MasterPage/User.Master"
            'Enable this line to prevent anonymous users from entering this page
            ' Response.Redirect("~/Login.aspx")
        End If
    End Sub
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
        If e.CommandName = "resolve" Then
            Session("ReportID") = 0
            Select Case e.Item.Cells(4).Text
                Case "Incorrect Information of Business"
                    Session("ReportID") = e.Item.Cells(2).Text
                    Response.Redirect("AdminUpdate.aspx?id=b" + e.Item.Cells(5).Text)
                Case "Proposed Deletion of Business"
                    Session("ReportID") = e.Item.Cells(2).Text
                    Response.Redirect("OutletReview.aspx?id=" + dbManager.getOutletIDFromBusinessID(Convert.ToInt32(e.Item.Cells(5).Text)).ToString())
                Case "Incorrect Information of Outlets"
                    Session("ReportID") = e.Item.Cells(2).Text
                    Response.Redirect("AdminUpdate.aspx?id=o" + e.Item.Cells(5).Text)
                Case "Proposed Deletion of Outlets"
                    Session("ReportID") = e.Item.Cells(2).Text
                    Response.Redirect("OutletReview.aspx?id=" + e.Item.Cells(5).Text)
                Case "Add Outlet"
                    Response.Redirect("OutletReview.aspx?id=" + e.Item.Cells(5).Text)
                Case "report comment"
                    Dim commentID As Integer = Convert.ToInt32(e.Item.Cells(5).Text)
                    Dim outletID As Integer = dbManager.getOutletIDFromComment(commentID)
                    If outletID > 0 Then
                        Session("ReportID") = e.Item.Cells(2).Text
                        Response.Redirect("OutletReview.aspx?id=" + outletID.ToString())
                    Else
                        dbManager.removeReportComment(commentID)
                        RadGrid1.DataBind()
                        MsgBox("Comment has already been deleted", MsgBoxStyle.OkOnly)
                    End If

                Case "report review"
                    Dim reviewID = Convert.ToInt32(e.Item.Cells(5).Text)
                    Dim outletID = dbManager.getOutletIDFromReview(reviewID)
                    If outletID > 0 Then
                        Session("ReportID") = e.Item.Cells(2).Text
                        Response.Redirect("OutletReview.aspx?id=" + outletID.ToString())
                    Else
                        'dbManager.removeReportReview(reviewID)
                        RadGrid1.DataBind()
                        MsgBox("Review has already been deleted", MsgBoxStyle.OkOnly)
                    End If
            End Select

        End If
    End Sub


    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
        lblError.Visible = False
        If (DropDownList1.SelectedValue.Equals("Email Address")) Then
            GridView2.DataSourceID = SqlDataSource1.ID
            GridView2.DataBind()
        Else
            GridView2.DataSourceID = SqlDataSource2.ID
            GridView2.DataBind()
        End If
        If (GridView2.Rows.Count <= 0) Then
            lblError.Visible = True
            lblError.Text = "Data is incorrect"
        End If
    End Sub

    Protected Sub GridView2_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView2.RowCommand
        Dim stringVal As String

        If e.CommandName = "Promote" Then

            If (DropDownList1.SelectedValue.Equals("Email Address")) Then

                SqlDataSource1.UpdateParameters("is_admin").DefaultValue = "true"
                GridView2.SelectRow(e.CommandArgument)
                Dim row As GridViewRow = GridView2.SelectedRow
                stringVal = row.Cells(0).Text

                SqlDataSource1.UpdateParameters("email").DefaultValue = stringVal
                SqlDataSource1.Update()

                GridView2.SelectRow(-1)

            ElseIf (DropDownList1.SelectedValue.Equals("Display Name")) Then

                SqlDataSource2.UpdateParameters("is_admin").DefaultValue = "true"
                GridView2.SelectRow(e.CommandArgument)
                Dim row As GridViewRow = GridView2.SelectedRow
                stringVal = row.Cells(0).Text

                SqlDataSource1.UpdateParameters("email").DefaultValue = stringVal
                SqlDataSource2.Update()

                GridView2.SelectRow(-1)
            End If

        ElseIf e.CommandName = "Demote" Then

            If (DropDownList1.SelectedValue.Equals("Email Address")) Then
                SqlDataSource1.UpdateParameters("is_admin").DefaultValue = "false"
                GridView2.SelectRow(e.CommandArgument)
                Dim row As GridViewRow = GridView2.SelectedRow
                stringVal = row.Cells(0).Text

                SqlDataSource1.UpdateParameters("email").DefaultValue = stringVal

                SqlDataSource1.Update()

                GridView2.SelectRow(-1)
            ElseIf (DropDownList1.SelectedValue.Equals("Display Name")) Then
                SqlDataSource2.UpdateParameters("is_admin").DefaultValue = "false"
                GridView2.SelectRow(e.CommandArgument)
                Dim row As GridViewRow = GridView2.SelectedRow
                stringVal = row.Cells(0).Text

                SqlDataSource1.UpdateParameters("email").DefaultValue = stringVal
                SqlDataSource2.Update()

                GridView2.SelectRow(-1)
            End If
        End If
    End Sub

    Protected Sub GridView2_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowCreated
        Dim row As GridViewRow = e.Row
        Dim columns As List(Of TableCell) = New List(Of TableCell)
        Dim cell As TableCell
        For Each column As DataControlField In GridView2.Columns
            cell = row.Cells(0)
            row.Cells.Remove(cell)
            columns.Add(cell)
        Next
        row.Cells.AddRange(columns.ToArray())

        Dim user As userAcc = Session("user")
        If user.superAdmin Then
            e.Row.Cells(5).Visible = True
            e.Row.Cells(6).Visible = True
        Else
            e.Row.Cells(5).Visible = False
            e.Row.Cells(6).Visible = False
        End If

    End Sub
End Class
